package com.qy566.gps.pulsar.api.impl;

import com.alibaba.fastjson.JSON;
import com.qy566.gps.common.base.logs.GpsLogger;
import com.qy566.gps.common.pulsar.errors.PulsarError;
import com.qy566.gps.common.requests.BaseResponse;
import com.qy566.gps.pulsar.api.PulsarController;
import com.qy566.gps.pulsar.entidy.Position;
import com.qy566.gps.pulsar.entidy.SendPulsarParams;
import com.qy566.gps.pulsar.producer.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author wangnaiqi
 * @description TODO
 * @date 2023/12/13
 */

@RestController
public class PulsarControllerImpl implements PulsarController {

    @Autowired
    private ProducerService producerService;

    @Override
    public BaseResponse<String> sendPulsar(SendPulsarParams sopRequest) {
        if (sopRequest.getMsg() == null) {
            return new BaseResponse<>(PulsarError.PULSAR_PRODUCER_ERROR, "发送消息不能为空");
        }
        String msg = JSON.toJSONString(sopRequest.getMsg());
        try {
            producerService.sendPulsar(sopRequest.getTopicName(), msg);
        } catch (Exception e) {
            GpsLogger.busLogger().error("消息发送Topic:" + sopRequest.getTopicName() + " 失败,失败原因： " + e.getMessage());
            return new BaseResponse<>(PulsarError.PULSAR_PRODUCER_ERROR, e.getMessage());
        }
        return new BaseResponse<>("消息发送成功" + msg);
    }

    @Override
    public BaseResponse<String> sendBatchPositionPulsar(Position position) {
        for (int y = 0; y <= 0; y++) {
            String key = null;
            switch (y) {
                case 0:
                    key = "A";
                    break;
                case 1:
                    key = "B";
                    break;
                case 2:
                    key = "C";
                    break;
            }
            producerService.generateCarNo(key,position);
        }

        return new BaseResponse<>("消息发送成功" + JSON.toJSONString(position));
    }

    @Override
    public BaseResponse<String> sendPositionPulsar(Position position) {
        producerService.sendPosition(position);
        return new BaseResponse<>("消息发送成功" + JSON.toJSONString(position));
    }

}
